package com.example.conusmer.service.impl


import com.example.common.entity.CustomerDubbo
import com.example.common.service.CustomerDubboService
import com.example.common.exception.DemoException
import com.example.conusmer.param.CustomerParams
import com.example.conusmer.entity.Customer
import com.example.conusmer.service.CustomerService
import org.apache.dubbo.config.annotation.DubboReference
import org.springframework.stereotype.Service
import java.util.*

@Service
class CustomerServiceImpl : CustomerService {
    @DubboReference()
    lateinit var customerService: CustomerDubboService
    override fun createCustomer(customer: Customer){
        customerService.createCustomer(CustomerDubbo(0,customer.name,customer.email))
    }
    override fun updateCustomer(customer: Customer) {
        customerService.updateCustomer(CustomerDubbo(customer.id!!,customer.name,customer.email))
    }


    override fun deleteCustomer(id: Long) {
        val existingCustomer = customerService.findCustomerById(id)
        if (existingCustomer.id!! >0) {
            customerService.deleteCustomer(id)
        } else {
            throw DemoException("Customer not exist")
        }

    }
    override fun findCustomerById(id: Long): Customer {
        val res = customerService.findCustomerById(id)
        return Customer(res.id,res.name,res.email)
    }

    override fun getAllCustomers(queryParams: CustomerParams): List<Customer> {
        val res =customerService.getAllCustomers(CustomerDubbo(0,queryParams.name,queryParams.email))
        return res.map { Customer(it.id,it.name,it.email) }
    }

}